package com.wl156.activities.order;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.xmlpull.v1.XmlPullParserException;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.wl156.BaseActivity;
import com.wl156.MyApp;
import com.wl156.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.widget.ProviceSelector;

public class DriverMyOrder extends BaseActivity implements OnClickListener {
	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;
	boolean mProgramStartFindResult = true;

	private ProviceSelector mFromProviceSelector;
	private ProviceSelector mToProviceSelector;
	private Button mConfirm;

	private EditText mCargosDescription;
	private EditText mCompanyName;
	private EditText mContactTel;
	private EditText mMerchandiser;
	private EditText mMerchandiserTel;
	private EditText mTonnage;
	private EditText mTruckTonnageSymbol;
	private EditText mAcceptDateStart;
	private EditText mAcceptDateEnd;
	private Spinner mOrderStatus;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
//		Window win = getWindow();
//		win.requestFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.act_driver_order);
//		win.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
//				R.drawable.ic_launcher);
		
		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		mFromProviceSelector = (ProviceSelector) findViewById(R.id.provice_selector);
		mFromProviceSelector.setTitle("起点: ");
		mFromProviceSelector.setProviceRequried(true);
		mToProviceSelector = (ProviceSelector) findViewById(R.id.to_provice_selector);
		mToProviceSelector.setTitle("终点: ");

		mConfirm = (Button) findViewById(R.id.confirm);
		mConfirm.setOnClickListener(this);

		mCargosDescription = (EditText) findViewById(R.id.CargosDescription);
		mCompanyName = (EditText) findViewById(R.id.companyName);
		mContactTel = (EditText) findViewById(R.id.contactTel);
		mTonnage = (EditText) findViewById(R.id.tonnage);
		mTruckTonnageSymbol = (EditText) findViewById(R.id.truckTonnageSymBol);
		mAcceptDateStart = (EditText) findViewById(R.id.acceptDateStart);
		mAcceptDateEnd = (EditText) findViewById(R.id.acceptDateEnd);
		mOrderStatus = (Spinner) findViewById(R.id.order_status);

		new StartTask().execute();

	}

	class StartTask extends AsyncTask<Void, Void, Object> {
		ProgressDialog mDialog;

		@Override
		protected void onPreExecute() {
			mDialog = ProgressDialog.show(DriverMyOrder.this, "", "加载中");
		};

		@Override
		protected void onPostExecute(Object result) {
			if (result instanceof Object[]) {
				Object[] values = (Object[]) result;
				SoapObject myCurrentPlace = (SoapObject) values[1];
				String lv1 = myCurrentPlace.getProperty(0).toString();
				String lv2 = myCurrentPlace.getProperty(1).toString();
				String lv3 = myCurrentPlace.getProperty(2).toString();
				mFromProviceSelector.setProvice(lv1);
				mFromProviceSelector.setCity(lv2);
				mFromProviceSelector.setRegion(lv3);

				if (!getIntent().getBooleanExtra("isReturn", false)) {
					mProgramStartFindResult = true;
					mConfirm.performClick();
				} else {
					mProgramStartFindResult = false;
				}
			} else {
				AlertDialog.Builder builder = new Builder(DriverMyOrder.this);
				builder.setMessage("网络请求超时！");
				builder.setPositiveButton("重试",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								new StartTask().execute();
							}
						});
				builder.setNegativeButton("取消",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								finish();
							}
						});
				builder.show();
			}

			mDialog.dismiss();
		};

		@Override
		protected Object doInBackground(Void... params) {
			try {
				Object[] values = new Object[2];
				values[1] = mWebServiceClient
						.commonGetMyCurrentPlace(mCertificate);
				return values;
			} catch (IOException e) {
				return e;
			} catch (XmlPullParserException e) {
				return e;
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.confirm:
			final String startLocationLv1 = mFromProviceSelector.getProvice();
			final String startLocationLv2 = mFromProviceSelector.getCity();
			final String startLocationLv3 = mFromProviceSelector.getRegion();
			final String endLocationLv1 = mToProviceSelector.getProvice();
			final String endLocationLv2 = mToProviceSelector.getCity();
			final String endLocationLv3 = mToProviceSelector.getRegion();
			final String freightMessageId = "";
			final String CargosDescription = mCargosDescription.getText()
					.toString();
			final String companyName = mCompanyName.getText().toString();
			final String contactTel = mContactTel.getText().toString();
			final String tonnage = mTonnage.getText().toString();
			final String truckTonnageSymBol = mTruckTonnageSymbol.getText()
					.toString();
			final String acceptDateStart = mAcceptDateStart.getText()
					.toString();
			final String acceptDateEnd = mAcceptDateEnd.getText().toString();
			final String[] orderStatus = { mOrderStatus.getSelectedItem()
					.toString() };

			Intent i = new Intent(this, DriverMyOrderResult.class);
			i.putExtra("startLocationLv1", startLocationLv1);
			i.putExtra("startLocationLv2", startLocationLv2);
			i.putExtra("startLocationLv3", startLocationLv3);
			i.putExtra("endLocationLv1", endLocationLv1);
			i.putExtra("endLocationLv2", endLocationLv2);
			i.putExtra("endLocationLv3", endLocationLv3);
			i.putExtra("freightMessageId", freightMessageId);
			i.putExtra("CargosDescription", CargosDescription);
			i.putExtra("LogisticCompanyName", companyName);
			i.putExtra("contactTel", contactTel);
			i.putExtra("tonnage", tonnage);
			i.putExtra("truckTonnageSymBol", truckTonnageSymBol);
			i.putExtra("acceptDateStart", acceptDateStart);
			i.putExtra("acceptDateEnd", acceptDateEnd);
			i.putExtra("orderStatus", orderStatus);
			startActivity(i);
			break;
		default:
			break;
		}
	}
}
